package com.mod;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author jianxy
 * @date 2025-08-19 22:26
 */
@Service
public class WebSocketService {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private ModbusService modbusService;

    // 定时推送数据到前端（每2秒一次）
    @Scheduled(fixedRate = 2000)
    public void pushPlcData() {
        short[] registers = modbusService.batchReadRegisters(1, 0, 10); // 读取10个寄存器
        messagingTemplate.convertAndSend("/topic/plc", registers);
    }
}
